﻿using System;
using System.Collections.Generic;
using System.Linq;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.DataAccess;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvUtilities;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
	/// <summary>
	///  ACS (American Community Survey) reporting methods.
	/// </summary>
    public class Report
    {
		/// <summary>
		/// Gets the ACS (American Community Survey) report definition.
		/// </summary>
		/// <returns>Object: <see cref="ACSDistrictSummaryReportDefinitionSTO"/></returns>
        public static ACSDistrictSummaryReportDefinitionSTO GetACSReportDefinition()
        {
            var result = new ACSDistrictSummaryReportDefinitionSTO();
			try
            {
                var dal = new DAL_CensusData();
                result.definitionList = dal.GetACSReportDefinition();
            }
            catch (Exception ex)
            {
                result.errorMessage = ex.Message;
            }
            return result;
		}

		/// <summary>
		/// Gets the ACS (American Community Survey) district data summary.
		/// </summary>
		/// <param name="iDistrictID">The idistrictID.</param>
		/// <param name="compressediRegionIDList">The compressediregionID list.</param>
		/// <param name="iSourceLayerID">The isourcelayerID.</param>
		/// <param name="iTableIDList">The itableID list.</param>
		/// <returns>Object: <see cref="ACSDistrictSummaryReportSTO"/></returns>
        public static ACSDistrictSummaryReportSTO GetACSDistrictDataSummary(int iDistrictID, byte[] compressediRegionIDList, string iSourceLayerID, string iTableIDList)
        {
            var result = new ACSDistrictSummaryReportSTO();
			try
            {
                var dal = new DAL_CensusData();
                var queryResults = dal.GetACSDictionaryIDs(iTableIDList);
                var x = from q in queryResults
                        select q;
                var numDictionaries = x.Count();


                var iSourceLayerIDList = "";
                for (int i = 0; i < numDictionaries; i++)
                {
                    if (numDictionaries == 1)
                    {
                        iSourceLayerIDList += iSourceLayerID;
                    }
                    else
                    {
                        if (i==numDictionaries-1)
                        {
                            iSourceLayerIDList += iSourceLayerID;
                        }
                        else
                        {
                            iSourceLayerIDList += iSourceLayerID + ",";
                        }
                    }
                }

                var dictionaryIDs = String.Join(",", x.ToArray());

                var iRegionIDList = CompressionManager.Decompress(compressediRegionIDList); //this is the regionID list
                if (iRegionIDList.Equals("")) //check to see if an empty region list is passed in
                {
                    result.dataSummary = new List<ACSDistrictSummaryDataDTO>();
                }
                else
                {
                    result.dataSummary = dal.GetACSDistrictDataSummary(iDistrictID, iRegionIDList, iSourceLayerIDList, dictionaryIDs);
                }
            }
            catch (Exception ex)
            {
                result.errorMessage = ex.Message;
            }
            return result;
        }
    }
}
